/**
* 表单提交
* */
$('.ajax-post').click(function(){
    //加载层
    var index = layer.load(2, {shade: [0.2,'#000']});

    var action = $(this).parents().attr("action");
    if(!action){
        var url = window.location.href;
    }else{
        //取出第一个字符
        var first = action.slice(0,1);
        if(first == '/'){
            var url = action;
        }else{
            var url = '/' + action
        }
    }

    var formData = $('form').serialize();
    $.post(url, formData, function (res){
        if(res.result == 1){
            setTimeout(function (){
                window.location.href = res.url;
            }, res.timer)
        }else if(res.result == 2){
            layer.close(index);
            layer.msg(res.msg);
            if(res.token) $("[name='__token__']").val(res.token);
        }
    },'json');

    setTimeout(function (){
        layer.close(index);
    },3000)
    return false;
});

/**
* 删除
* */
$('.ajax-del').click(function (){
    var url = $(this).attr("href");

    swal({
        title: "您确认删除吗？",
        type: "warning",
        cancelButtonText: '取消',
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确认",
        closeOnConfirm: false
    }, function () {
        // 加载层
        var index = layer.load(2, {shade: [0.2,'#000']});

        $.get(url, function (res){
            if(res.result == 1){
                setTimeout(function (){
                    window.location.href = res.url;
                }, res.timer)
            }else if(res.result == 2){
                layer.close(index);
                layer.msg(res.msg);
            }
        },'json');

        setTimeout(function (){
            layer.close(index);
        },3000)
    });
    return false;
})

/**
 * 操作
 * */
$('.ajax-operate').click(function (){
    console.log(1)
    var url = $(this).attr("href");

    swal({
        title: "您确认要执行该操作吗？",
        type: "warning",
        cancelButtonText: '取消',
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确认",
        closeOnConfirm: false
    }, function () {
        // 加载层
        var index = layer.load(2, {shade: [0.2,'#000']});

        $.get(url, function (res){
            if(res.result == 1){
                setTimeout(function (){
                    window.location.href = res.url;
                }, res.timer)
            }else if(res.result == 2){
                layer.close(index);
                layer.msg(res.msg);
            }
        },'json');

        setTimeout(function (){
            layer.close(index);
        },3000)
    });
    return false;
})

/**
* 返回上一页
* */
$('.back-page').click(function (){
    window.history.go(-1);
})

/**
* 清理缓存
* */
$('#clear-cache').click(function (){
    var url = "/admin/Func/clearCache";
    $.post(url, function (){
        layer.msg('缓存清理成功')
    },'json')
})

/**
* 退出登录
* */
$('#logout').click(function (){
    var url = "/admin/Func/logout";
    $.post(url, function (res){
        layer.msg(res.msg)
        setTimeout(function (){
            window.location.href = res.data;
        },res.timer)
    },'json')
})

$('#confirmUpdateAdminPwd').click(function (){
    var old_admin_pwd = $('.old_admin_pwd').val();
    var new_admin_pwd = $('.new_admin_pwd').val();
    var re_admin_pwd = $('.re_admin_pwd').val();

    if(!old_admin_pwd){
        layer.msg('请输入原密码');
        return;
    }
    if(!new_admin_pwd){
        layer.msg('请输入新密码');
        return;
    }
    if(!re_admin_pwd){
        layer.msg('请输入确认密码');
        return;
    }
    if(new_admin_pwd !== re_admin_pwd){
        layer.msg('两次密码不一致，请重新输入');
        return;
    }

    var url = "/admin/func/updateAdminPwd";

    $.post(url, {
        old_admin_pwd: old_admin_pwd,
        new_admin_pwd: new_admin_pwd
    }, function (res){
        layer.msg(res.msg)
        if(res.result == 1){
            $(".close-model").trigger("click");
        }
    },'json')
})